package com.java01.apache.commons.exec;

import java.io.IOException;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.util.StringUtils;

public class ExternalCommand2 {

	public static void main(String[] args) {
		String userHomePath = System.getProperty("user.home");
		System.out.println("user home>> "+userHomePath);
		
		String line = "explorer "+StringUtils.quoteArgument(userHomePath);
		System.out.println("command>> "+line);
		
		CommandLine commandLine = CommandLine.parse(line);
		DefaultExecutor executor = new DefaultExecutor();
		
		//Explorer returned an exit value of '1' on success which is usually considered as an execution failure.
		//setExitValue(1) resolves this problem.
		executor.setExitValue(1);
		try {
			int exitValue = executor.execute(commandLine);
			System.out.println(exitValue);
		} catch (ExecuteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
